package fr.ants.model.stats;

import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import au.com.bytecode.opencsv.CSVWriter;

public class StatsToCsv {

	private static final String[] CYCLE_COLUMNS = new String[] {"Cycle", "Nests", "Food in nests", "Food in transit", "Food available", "Steps exploring", "Steps following", "Steps returning", "Crops", "Deposits"};

	public static final void export(SimulatorStatistics stats, String path) {
		DateFormat dateFormater = new SimpleDateFormat("dd-MM-yyyy_HH-mm-ss");
		try {
			CSVWriter writer = new CSVWriter(new FileWriter(path+dateFormater.format(new Date()) + ".csv"));
			writeCycleStats(stats, writer);
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void writeCycleStats(SimulatorStatistics stats, CSVWriter writer) {
		writer.writeNext(CYCLE_COLUMNS);
		int cycleId = 0;
		for (CycleStatistic cycle : stats.getCycleStats()) {
			writer.writeNext(new String[] {String.valueOf(cycleId), String.valueOf(cycle.getNestResources()),
					String.valueOf(cycle.getNestStats().size()), String.valueOf(cycle.getResourcesInTransit()),
					String.valueOf(cycle.getResourcesAvailable()), String.valueOf(cycle.getExploreSteps()),
					String.valueOf(cycle.getFollowSteps()), String.valueOf(cycle.getToNestSteps()),
					String.valueOf(cycle.getCrops()), String.valueOf(cycle.getDeposits())});
			cycleId++;
		}
	}
}
